<?php

namespace app\common\model\Project;

use traits\model\SoftDelete;
use think\Model;

class ProjectOrder extends Model
{
    //
    protected $table = "pre_project_order";

    // TODO 开启软删除
    use SoftDelete;
    protected $deleteTime = 'deletetime';

    // 追加字段
    protected $append = [
        'status_text',
        'status_class',
        'createtime_text',
        'region_text',
        'provinces_list',
        'citys_list',
        'districts_list',
    ];

    protected function getStatusTextAttr($value, $data)
    {
        $status = $data['status'];
        $text = '';

        switch ($status) {
            case 0:
                $text = '未接单';
                break;
            case 1:
                $text = '待付款';
                break;
            case 2:
                $text = '已付款，可申请撤销';
                break;
            case 3:
                $text = '已评价';
                break;
            case -1:
                $text = '已退货';
                break;
            case -2:
                $text = '管理员审核';
                break;
            case -3:
                $text = '拒绝撤销';
                break;
            default:
                $text = '未知状态';
        }

        return $text;
    }

    protected function getStatusClassAttr($value, $data)
    {
        $status = $data['status'];
        $text = '';

        switch ($status) {
            case 0:
                $text = 'badge badge-warning';
                break;
            case 1:
                $text = 'badge badge-success';
                break;
            case 2:
                $text = 'badge badge-info';
                break;
            case 3:
                $text = 'badge badge-cyan';
                break;
            case -1:
                $text = 'badge badge-pink';
                break;
            case -2:
                $text = 'badge badge-dark';
                break;
            case -3:
                $text = 'badge badge-danger';
                break;
            default:
                $text = 'badge badge-muted';
        }

        return $text;
    }

    // TODO 所在地
    public function getRegionTextAttr($value, $data)
    {
        $address = model('User.Address')->where('id', $data['addrid'])->find();
        $ReogionList = model('Region')->where(['code' => $address['district']])->find();

        $region = $ReogionList['province'] . $ReogionList['city'] . $ReogionList['district'] . $address['address'];

        return $region;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        return date('Y-m-d H:i', $data['createtime']);
    }

    public function getProvincesListAttr($value, $data)
    {
        // TODO 刚开始，查询地区省份
        $provinces = model('Region')->where(['grade' => 1])->select();

        return $provinces;
    }

    public function getCitysListAttr($value, $data)
    {
        $address = model('User.Address')->where('id', $data['addrid'])->find();

        // TODO 刚开始，查询城市
        if ($address['province']) {
            $citys = model('Region')->where(['parentid' => $address['province']])->select();
        } else {
            $citys = [];
        }
        return $citys;
    }

    public function getDistrictsListAttr($value, $data)
    {
        $address = model('User.Address')->where('id', $data['addrid'])->find();

        // TODO 刚开始，区数据
        if ($address['city']) {
            $districts = model('Region')->where(['parentid' => $address['city']])->select();
        } else {
            $districts = [];
        }
        return $districts;
    }

    public function getProjectListAttr($value,$data){
        $projectList = model('Project.Project')->select();

        return $projectList;
    }


    public function project()
    {
        return $this->belongsTo('app\common\model\Project\Project', 'projectid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User\User', 'userid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function address()
    {
        return $this->belongsTo('app\common\model\User\Address', 'addrid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
